def pre_fmt(data):
    return data.replace("_{_", "_1").replace("_}_", "_2")


def after_fmt(data):
    return data.replace("_1", "{").replace("_2", "}")


def sql_create_note(table_name):
    return "/*以下为生成{0}表的sql语句*/".format(table_name)


def sql_create_table(table_name, content):
    return "create table {table_name}({content});".format(table_name=table_name, content=content)


def sql_create_table_col(field_code, field_type, field_restrain, table_primary):
    return "{field_code} {field_type} {field_restrain} {table_primary}".format(field_code=field_code,
                                                                               field_type=field_type,
                                                                               field_restrain=field_restrain,
                                                                               table_primary=table_primary)


def sql_create_comment(table_name, field_code, field_name):
    return "comment on column \"{table_name}\".\"{field_code}\" is '{field_name}';".format(table_name=table_name,
                                                                                           field_code=field_code,
                                                                                           field_name=field_name)


def sql_create_seq(seq_name):
    return "CREATE SEQUENCE {seq_name} INCREMENT BY 1 START WITH 1 NOMAXVALUE NOCYCLE NOCACHE;".format(
        seq_name=seq_name)


def sql_create_trigger(table_name, trigger_name, seq_name):
    return """CREATE or REPLACE TRIGGER {trigger_name} BEFORE INSERT ON {table_name} FOR EACH ROW WHEN (new.id is null) 
                begin 
                select {seq_name}.nextval into: new.id from dual; 
                end;""".format(trigger_name=trigger_name, table_name=table_name, seq_name=seq_name)


def sql_create_one_to_all(note_sql, table_sql, comment_sql, seq_sql, trigger_sql):
    return "{note_sql}\n{table_sql}\n{comment_sql}\n{seq_sql}\n{trigger_sql}\n".format(note_sql=note_sql,
                                                                                       table_sql=table_sql,
                                                                                       comment_sql=comment_sql,
                                                                                       seq_sql=seq_sql,
                                                                                       trigger_sql=trigger_sql)
